/**
 * 
 */
package com.smarthome.api.mobile.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.smarthome.common.mobile.Service;
import com.smarthome.common.tools.APIUtil;
import com.smarthome.core.housemanager.ResidenceFeeService;
import com.smarthome.core.housemanager.ResidenceManagerService;
import com.smarthome.domain.bean.ShAccount;
import com.smarthome.domain.bean.ShResidence;
import com.smarthome.domain.common.Constants;
import com.smarthome.domain.mobile.MobileCodeEnum;
import com.smarthome.domain.mobile.MobileResponse;
import com.smarthome.domain.mobile.req.MobileResidenceRequest;


/**
 * @author dhc
 * @date 2015年10月19日 上午9:25:31
 * @title 小区管理
 */
@org.springframework.stereotype.Service("mobile.residence.service")
public class MobileResidenceService implements Service<MobileResponse<?>> {
	
	@Resource(name="residenceManagerService")
	private ResidenceManagerService residenceManagerService;
	
	@Resource(name="residenceFeeService")
	private ResidenceFeeService feeService;
	
	/**
	 * @title 显示小区列表，如果当前用户登录，获取当前登录用户小区，否则获取所有小区
	 * @createTime 2015年10月19日 下午3:29:56
	 * @author dhc
	 */
	public void showList(MobileResidenceRequest request,MobileResponse<Object> response){
		ShAccount account = request.getAccount();
		if(APIUtil.isLogin(request)){
			residenceManagerService.resetResidenceForAccount(request.getAccount());
		}
		List<ShResidence> residences = null;
		if(null != account && account.getResidenceId().longValue() != 0l){
			residenceManagerService.resetResidenceForAccount(account);
			residences = account.getResidences();
		}
		
		if(null == residences || residences.size() == 0){
			ShResidence residence = new ShResidence();
			//residence.setAreaId(request.getAreaId());
			residences = residenceManagerService.queryShResidences(residence);
		}
		
		response.setData(residences);
		
	}
	
	/**
	 * @title 获取所有小区列表
	 * @createTime 2015年10月19日 上午11:54:33
	 * @author dhc
	 */
	public void all(MobileResidenceRequest request,MobileResponse<Object> response){
		response.setData(residenceManagerService.queryShResidences(null));
	}
	
	/**
	 * @title 查询小区
	 * @createTime 2015年10月24日 下午5:25:00
	 * @author dhc
	 */
	public void list(MobileResidenceRequest request,MobileResponse<Object> response){
		ShResidence residence = new ShResidence();
		residence.setId(request.getResidenceId());
		residence.setName(request.getResidenceName());
		residence.setAreaId(request.getAreaId());
		residence.setCityId(request.getCityId());
		residence.setProviceId(request.getProviceId());
		List<ShResidence> residences = residenceManagerService.queryShResidences(residence);
		
		List<Map<String, Object>> rs = new ArrayList<Map<String, Object>>();
		
		if(null != residence){
			for(ShResidence r : residences){
				if(Constants.PLATFORM_MANAGE_RESIDENCE_ID.equals(r.getId())) continue;
				Map<String,Object> map = new HashMap<String, Object>();
				map.put("id", r.getId());
				map.put("name", r.getName());
				map.put("areaId", r.getAreaId());
				map.put("address", r.getAddress());
				rs.add(map);
			}
		}
		
		response.setData(rs);
	}
	
	
	
	/**
	 * @title 获取当前用户小区
	 * @createTime 2015年10月19日 上午10:51:57
	 * @author dhc
	 */
	public void current(MobileResidenceRequest request, MobileResponse<ShResidence> response){
		if(!APIUtil.isLogin(request)){
			response.setMobileCodeObj(MobileCodeEnum.NOT_LOGIN_OPTION);
			return ;
		}
		ShAccount account = request.getAccount();
		Long residenceId = account.getResidenceId();
		if(null == residenceId){
			response.setMobileCodeObj(MobileCodeEnum.VISIT_RESTRICT);
			return ;
		} else if(residenceId.longValue() == 0){
			response.setData(new ShResidence());
		}
		ShResidence residence = residenceManagerService.queryResidenceById(residenceId);
		if(null == residence) residence =new ShResidence();
		residence.setResidenceFee( feeService.residenceFeeById(residenceId));
		response.setData(residence);
	}
	
	/**
	 * @title 获当前用户所有的小区
	 * @createTime 2015年10月19日 上午10:59:25
	 * @author dhc
	 */
	public void owenresidences(MobileResidenceRequest request, MobileResponse<Object> response){
		if(!APIUtil.isLogin(request)){
			//response.setMobileCodeObj(MobileCodeEnum.NOT_LOGIN_OPTION);
			List<ShResidence> residences = new ArrayList<ShResidence>();
			ShResidence r = new ShResidence();
			r.setId(0l);
			r.setName("未登录用户");
			residences.add(r);
			response.setData(residences);
			return ;
		}
		
		ShAccount account = request.getAccount();
		residenceManagerService.resetResidenceForAccount(account);
		
		List<ShResidence> rs = account.getResidences();
		for(int i=1;i<rs.size();i++){
			ShResidence r = rs.get(i);
			if(account.getId() == r.getId()){
				ShResidence at =  rs.get(0);
				rs.set(0, r);				
				rs.set(i, at);
				break;
			}
		}
		
		response.setData(rs);
	}
	
	/**
	 * @title 根据小区Id，获取小区
	 * @createTime 2015年10月19日 上午11:02:01
	 * @author dhc
	 */
	public void detail(MobileResidenceRequest request, MobileResponse<ShResidence> response){
		Long residenceId = request.getResidenceId();
		if(null == residenceId){
			response.setMobileCodeObj(MobileCodeEnum.ACCOUNT_NOT_FULL);
			return ;
		}
		response.setData(residenceManagerService.queryResidenceById(residenceId));	
		
	}
	

	/**
	 * @title 切换小区
	 * @createTime 2015年10月19日 上午11:51:00
	 * @author dhc
	 */
	public void change(MobileResidenceRequest request,MobileResponse<ShResidence> response){
		//验证登录
		if(!APIUtil.isLogin(request)){
			response.setMobileCodeObj(MobileCodeEnum.NOT_LOGIN_OPTION);
			return ;
		}
		//验证参数
		Long residenceId = request.getResidenceId();
		
		if(null == residenceId){
			response.setMobileCodeObj(MobileCodeEnum.ACCOUNT_NOT_FULL);
			return ;
		}
		
		ShAccount account = request.getAccount();
		residenceManagerService.changeCurrentResidenceForAccount(account, residenceId);
		response.setData(residenceManagerService.queryResidenceById(residenceId));
		
	}
	
	/**
	 * @title 重置平台用户小区列表
	 * @createTime 2015年10月19日 上午11:59:44
	 * @author dhc
	 */
	public void  reset(MobileResidenceRequest request,MobileResponse<Object> response){
		if(!APIUtil.isLogin(request)){
			response.setMobileCodeObj(MobileCodeEnum.NOT_LOGIN_OPTION);
			return ;
		}
		ShAccount account = request.getAccount();
		residenceManagerService.resetResidenceForAccount(account);		
		
		response.setData(account.getResidences());
		
	}
	
	
}
